package hook;

import sun.misc.Signal;
import sun.misc.SignalHandler;

import java.util.concurrent.TimeUnit;

public class Lesson2 {
    public static void main(String[] args) {
        Signal signal = new Signal(getOSSignalType());
        Signal.handle(signal, signal1 -> {
            //此处如果被阻塞则后面的其他操作都将无法执行包括  Runtime.getRuntime().addShutdownHook
            /*try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*/
            System.out.println("signal" + signal1.getNumber());
        });

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static String getOSSignalType() {
        return System.getProperties().getProperty("os.name").toLowerCase().startsWith("win") ? "INT" : "TERM";
    }
}
